﻿using AddrDBContext.Model;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.SQLite.EF6;
using System.Linq;
using System.Text;

namespace AddrDBContext
{
    public class AddressDBContext : DbContext
    {
        public static AddressDBContext CreatInstance(string connString)
        {
            DbConnection sqliteCon = SQLiteProviderFactory.Instance.CreateConnection();
            sqliteCon.ConnectionString = connString;
            return new AddressDBContext(sqliteCon); ;
        }
        private AddressDBContext(DbConnection con) : base(con, true) { }
        public DbSet<TStreet> TStreets { get; set; }
        public DbSet<TAddress> TAddresss { get; set; }
        public DbSet<TRoad> TRoads { get; set; }
        public DbSet<TCommunity> TCommunitys { get; set; }

        public T AddOrUpdate<T>(T t) where T : class, new()
        {
            if(t is TAddress)
            {
                TAddress address = t as TAddress;
                var addresss = TAddresss.Where(r => r.Code == address.Code || r.XZ == address.XZ);
                if (addresss.Count() == 0)
                {
                    TAddress addrNew = TAddresss.Add(address);
                    return addrNew as T;
                }
                else if (addresss.Count() > 0)
                {
                    TAddress addrMod = addresss.First();
                    TAddresss.Attach(addrMod);
                    addrMod.Community = address.Community;
                    addrMod.FJID = address.FJID;
                    addrMod.XZ = address.XZ;
                    addrMod.FJID = address.FJID;
                    addrMod.Remarks = address.Remarks;
                    //SaveChanges();
                    return addrMod as T;
                }
                return null;
            }
            else if(t is TStreet)
            {
                TStreet street = t as TStreet;
                var streets = TStreets.Where(r=>r.Name == street.Name);
                if (streets.Count() == 0)
                {
                    TStreet streetNew = TStreets.Add(street);
                    //SaveChanges();
                    return streetNew as T;
                }
                else if (streets.Count() > 0)
                {
                    TStreet streetMod = streets.First();
                    TStreets.Attach(streetMod);
                    streetMod.Code = street.Code;
                    streetMod.Remarks = street.Remarks;
                    //SaveChanges();
                    return streetMod as T;
                }
                return null;
            }
            if(t is TCommunity)
            {
                TCommunity community = t as TCommunity;
                var communitys = TCommunitys.Where(r => r.Name == community.Name 
                && r.MPH == community.MPH
                && r.Road == community.Road);
                if (communitys.Count() == 0)
                {
                    TCommunity communityNew = TCommunitys.Add(community);
                    //SaveChanges();
                    return communityNew as T;
                }
                else if (communitys.Count() > 0)
                {
                    TCommunity communityMod = communitys.First();
                    TCommunitys.Attach(communityMod);
                    communityMod.Code = community.Code;
                    communityMod.Street = community.Street;
                    communityMod.RoadCode = community.RoadCode;
                    communityMod.StreetCode = community.StreetCode;
                    communityMod.Remarks = community.Remarks;
                    //SaveChanges();
                    return communityMod as T;
                }
                return null;
            }
            if(t is TRoad)
            {
                TRoad road = t as TRoad;
                var roads = TRoads.Where(r=>r.Street == road.Street && r.Name == road.Name);
                if(roads.Count() == 0)
                {
                    TRoad roadNew = TRoads.Add(road);
                    //SaveChanges();
                    return roadNew as T;
                }
                else if(roads.Count() > 0)
                {
                    TRoad roadMod = roads.First();
                    TRoads.Attach(roadMod);
                    roadMod.Code = road.Code;
                    roadMod.Remarks = road.Remarks;
                    roadMod.StreetCode = road.StreetCode;
                    //SaveChanges();
                    return roadMod as T;
                }
                return null;
            }
            return null;
        }
    }
}
